<template>
	<view>
		<view>
			<u-cell-group title="模块列表 ·">
				<u-table>
					<u-tr>
						<u-th>序号</u-th>
						<u-th>内容</u-th>
						<u-th>操作</u-th>
					</u-tr>
					<view v-for="(item,index) in modules" :key="index">
						<u-tr>
							<u-td>{{index+1}}</u-td>
							<u-td>{{item.moduleName}}</u-td>
							<u-td><p @click="deleteModule(item)">删除</p></u-td>
						</u-tr>
					</view>
				</u-table>
			</u-cell-group>
		</view>
		<view>
			<u-modal v-model="show" :title-style="{color: 'red'}" title="请输入模块" show-cancel-button="true" @confirm="sure()">
				<view class="slot-content" style="padding: 20rpx;margin: 20rpx 0;">
					<u-input v-model="inputValue" :type="type" :border="border" label="模块"/>
				</view>
			</u-modal>
			<u-modal v-model="tip" :content="content"></u-modal>
			<u-modal v-model="sureFlag" :content="scontent" show-cancel-button="true" @confirm="sureDelete()"></u-modal>
		</view>
		<view>
			<view class="btn">
				<u-button class="u-m-r-5" @click="addModule()" plain type="primary">增加模块</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import api from '../../utils/api.js'
	export default {
		data() {
			return {
				sureFlag:false,
				scontent:'',
				title:"请输入模块",
				tip:false,
				content:'',
				flag:false,
				show:false,
				inputValue: '',
				type: 'text',
				border: true,
				modules:[],
				temp:[]
			}
		},
		created(){
			this.requestData()
		},
		methods: {
			requestData(){
				api.getAllModules().then(res => {
					console.log(res)
					this.modules = res.data
				})
			},
			sure:function(){
				let user = uni.getStorageInfoSync("user")
				var module = {
					branchId:user.branchId,
					moduleName:this.inputValue
				}
				console.log(this.inputValue)
				if (module.moduleName != '' &&  module.moduleName != undefined && module.moduleName != null){
					api.addModule(module).then(res => {
						if (res.code === 0){
							this.requestData()
							this.tip = true
							this.content = '添加成功'
						}else{
							this.tip = true
							this.content = '系统出错'
						}
					})
				}else{
					this.tip = true
					this.content = '输入的值不能为空'
				}
				
			},
			addModule:function(){
				this.show = true
			},
			sureDelete:function(){
				api.deleteModule(this.temp.id).then(res => {
					if (res.code === 0){
						this.requestData()
						this.tip = true
						this.content = '删除成功'
					}else{
						this.tip = true
						this.content = '系统出错'
					}
				})
			},
			deleteModule:function(item){
				this.temp = item
				this.sureFlag = true
				this.scontent = "确定删除吗？"
			}
		}
	}
</script>

<style>
.btn{
		margin: 0 auto;
		width: 60%;
		margin-top: 30rpx;
	}
</style>
